package com.yzy.fly.service.util;

import com.google.common.collect.Lists;
import com.yzy.fly.entity.core.FlyClass;
import com.yzy.fly.entity.core.FlyClassCascade;
import com.yzy.fly.entity.core.FlyField;
import com.yzy.fly.entity.core.FlyFieldCascade;
import com.yzy.fly.entity.role.FlyAttribute;
import com.yzy.fly.entity.role.FlyModel;
import com.yzy.fly.type.JDKVersionType;
import com.yzy.fly.util.ListUtil;

import java.util.List;

public class FlyModelConfigUtil {
//    @Autowired
//    private FlyModelMapper flyModelMapper;

    public static class ConfigConvert{
        public static List<FlyClass> modelToClass(FlyModel model) {
            FlyClassCascade flyClassCascade = modelToClassCascade(model);
            List<FlyClass> flyClasses = classCascadeToClass(flyClassCascade);
            return flyClasses;
        }

        public static FlyClass modelToClassMain(FlyModel model) {
            FlyClassCascade flyClassCascade = modelToClassCascade(model);
            FlyClass flyClass = classCascadeToClassMain(flyClassCascade);
            return flyClass;
        }

        /**
         *
         * @param model
         * @return
         */
        public static FlyClassCascade modelToClassCascade(FlyModel model) {
            FlyClassCascade flyClass = new FlyClassCascade();
            flyClass.setVersion(model.getVersion());
            flyClass.setAccess(model.getAccess());
            flyClass.setPackageName(model.getPackageName());
            flyClass.setClassName(model.getClassName());
            flyClass.setName(model.getName());
//            flyClass.setSignature();
            String superName = model.getPackageSuperName()+"."+model.getSuperName();
            if (".".equals(superName)) {
                superName = "";
            }
            flyClass.setSuperName(superName);
//            flyClass.setInterfaces();
//            flyClass.setChildrens();
//            flyClass.setAnnotationList();
//            flyClass.setSqlList();
            flyClass.setParentId(model.getParentId());

            //数据库设置
            model.getTableName();
            //字段设置
            List<FlyAttribute> attrs = model.getAttrs();
            if (ListUtil.listNotEmpty.apply(attrs)) {
                List<FlyFieldCascade> flyFieldList = Lists.newArrayList();
                for (int i = 0; i < attrs.size(); i++) {
                    FlyAttribute flyAttribute = attrs.get(i);
                    FlyFieldCascade flyField = new FlyFieldCascade();
                    flyField.setSeq(flyAttribute.getSeq());
                    flyField.setAccess(flyAttribute.getAccess());
                    flyField.setName(flyAttribute.getFieldName());
//                    flyField.setDescriptor();
//                    flyField.setSignature();
//                    flyField.setValue();
//                    flyField.setClassId();
//                    flyField.setAnnotationList();
                    flyField.setAttrId(flyAttribute.getId());
                    flyField.setModelId(flyAttribute.getModelId());
                    flyField.setDefModelId(flyAttribute.getDefModelId());
                    flyField.setSystemId(flyAttribute.getSystemId());
                    flyFieldList.add(flyField);
                }
                flyClass.setFieldList(flyFieldList);
            }
            List<FlyModel> childrens = model.getChildrens();
            if (ListUtil.listNotEmpty.apply(childrens)) {
                List<FlyClassCascade> flyClassList = Lists.newArrayList();
                for (int i = 0; i < childrens.size(); i++) {
                    FlyModel flyModel = childrens.get(i);
                    FlyClassCascade subFlyClass = modelToClassCascade(flyModel);
//                FlyFieldUtil::convertFromClass(subFlyClass);
                    flyClassList.add(subFlyClass);
                }
                flyClass.setChildrens(flyClassList);
            }
            return flyClass;
        }

        public static List<FlyClass> classCascadeToClass(FlyClassCascade classCascade) {
            List<FlyClass> flyClassList = Lists.newArrayList();
            classCascadeToClassDetail(flyClassList,classCascade);
            return flyClassList;
        }

        public static void classCascadeToClassDetail(List<FlyClass> flyClassList,FlyClassCascade classCascade) {
            FlyClass flyClass = new FlyClass();
            flyClass.setVersion(JDKVersionType.value(classCascade.getVersion()));
            flyClass.setAccess(classCascade.getAccess());
            flyClass.setPackageName(classCascade.getPackageName());
            flyClass.setClassName(classCascade.getClassName());
            flyClass.setName(classCascade.getName());
//            flyClass.setSignature();
            flyClass.setSuperName(classCascade.getSuperName());

            //数据库设置
//        classCascade.getTableName();
            //字段设置
            List<FlyField> flyFields = Lists.newArrayList();
            List<FlyFieldCascade> flyFieldList = classCascade.getFieldList();
            if (ListUtil.listNotEmpty.apply(flyFieldList)) {

                for (int i = 0; i < flyFieldList.size(); i++) {
                    FlyFieldCascade flyFieldCascade =  flyFieldList.get(i);

                    FlyField flyField = new FlyField();
                    flyField.setSeq(flyFieldCascade.getSeq());
                    flyField.setAccess(flyFieldCascade.getAccess());
                    flyField.setName(flyFieldCascade.getName());
                    flyField.setDescriptor(flyFieldCascade.getDescriptor());
//                    flyField.setSignature();
//                    flyField.setValue();
//                    flyField.setAnnotationList();
                    flyFields.add(flyField);
                }

            }
            List<FlyClassCascade> childrens = classCascade.getChildrens();
            if (ListUtil.listNotEmpty.apply(childrens)) {
                for (int i = 0; i < childrens.size(); i++) {
                    FlyClassCascade flyClassCascade =  childrens.get(i);
                    FlyClass flyClass1 = classCascadeToClassMain(flyClassCascade);
                    FlyField flyField = FlyFieldUtil.convertFromClass(flyClass1);
                    flyFields.add(flyField);
                }
            }
            if (ListUtil.listNotEmpty.apply(flyFields)) {
                flyClass.setFieldList(flyFields);
            }
            flyClassList.add(flyClass);
            if (ListUtil.listNotEmpty.apply(childrens)) {
                for (int i = 0; i < childrens.size(); i++) {
                    FlyClassCascade flyClassCascade =  childrens.get(i);
                    classCascadeToClassDetail(flyClassList,flyClassCascade);
                }
            }
        }

        public static FlyClass classCascadeToClassMain(FlyClassCascade classCascade) {
            FlyClass flyClass = new FlyClass();
            List<FlyField> flyFields = Lists.newArrayList();

            List<FlyFieldCascade> flyFieldList = classCascade.getFieldList();

            if (ListUtil.listNotEmpty.apply(flyFieldList)) {
                for (int i = 0; i < flyFieldList.size(); i++) {
                    FlyFieldCascade flyFieldCascade =  flyFieldList.get(i);
                    FlyField flyField = new FlyField();

                    flyFields.add(flyField);
                }
            }
            List<FlyClassCascade> childrens = classCascade.getChildrens();
            if (ListUtil.listNotEmpty.apply(childrens)) {
                for (int i = 0; i < childrens.size(); i++) {
                    FlyClassCascade flyClassCascade =  childrens.get(i);
                    FlyField flyField = new FlyField();

                    flyFields.add(flyField);
                }
            }
            if (ListUtil.listNotEmpty.apply(flyFields)) {
                flyClass.setFieldList(flyFields);
            }
            return flyClass;
        }
    }

}